package com.qf.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import com.qf.feign.SearchFeign;
import com.qf.pojo.Customer;
import com.qf.service.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 *  测试远程调用 请求缓存
 * @author lixu
 */
@Service
public class CacheServiceImpl implements CacheService {


    @Autowired
    private SearchFeign searchFeign;

    /**
     * 测试缓存    缓存中：Key的一部分 ：  包名+类名 + 方法名 + 形参
     * @param id
     * @return
     */
    @Override
    @CacheResult  //请求缓存注解
    @HystrixCommand(commandKey = "findCustomerById")
    public Customer findCustomerById(@CacheKey String id) {
        //远程调用
        return searchFeign.findById(Integer.parseInt(id));
    }

    /**
     * 清理缓存
     * @param id
     */
    @Override
    @CacheRemove(commandKey = "findCustomerById")
    @HystrixCommand
    public void removeCache(@CacheKey String id) {
        System.out.println("缓存清空了");
    }
}
